// order.js
var util = require('../../utils/util.js');
var pay = require('../../utils/pay.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: getApp().globalData.host,
    platUser: {},
    orderId:'',
    order:{},
    hidden: false,
    scrollHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.setData({
      platUser:getApp().globalData.platUser,
      orderId: typeof(options.orderId) == "undefined"?'':options.orderId
    }) 
    this.getData();
  },

  getData: function () {
    var that = this;
    wx.request({
      //url: that.data.host + 'webapp/orders/detail.html',
      url: that.data.host + 'order/orderToConfirm',
      data: { orderId: that.data.orderId},
      header: getApp().globalData.header,
      method:'POST',
      success: function (res) {
        var tempOrder = res.data.order;
        tempOrder.orderSkus = res.data.skuMaps;
        if(typeof(tempOrder.orderSkus)!='undefined'&&tempOrder.orderSkus.length>0){
            for(let j = 0;j<tempOrder.orderSkus.length;j++){
              tempOrder.orderSkus[j].imgUrl = util.imageUrl(tempOrder.orderSkus[j].imgUrl);
            }
          }
        tempOrder.createTime = util.formatTimeStamp(tempOrder.createTime);
        tempOrder.payTime = util.formatTimeStamp(tempOrder.payTime);
        tempOrder.deliveryTime = util.formatTimeStamp(tempOrder.deliveryTime);
        tempOrder.receivingTime = util.formatTimeStamp(tempOrder.receivingTime);
        that.setData({
          order: tempOrder,
          hidden: true
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  
  //获取收货地址
  getAddress:function(){
    var that = this;
    wx.request({
      url: that.data.host + 'address/queryAddress.html',
      data: { addressId: that.data.order.deliveryAddressId},
      header: getApp().globalData.header,
      success: function (res) {
        that.order.userAddress = res.data
        that.setData({
          order:  that.order
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })    
  },
  showExpress:function(){
    wx.navigateTo({
      url: '../express/express?orderId=' + this.data.orderId,
    })
  },
  //去付款
  doPay:function(e){
    //console.log("index is : " + index + "| order is : " + this.data.orders[index].orderCode);
    //待付款订单无需收货地址
    pay.toPay(this.data.order,'');
  },
  //确认收货
  confirmDelivery:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认已收到商品？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: that.data.host + 'bizportal/my/buyer/order/confirmDelivery',
            data: { orderId: that.data.orderId},
            header: getApp().globalData.header,
            method: 'POST',
            success: function (res) {
              if(res.data.errorCode==0){
                that.data.order.status = 'Received';
                that.setData({
                  order: that.data.order
                });
              }else{
                wx.showToast({
                  title: "确认失败",
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { }
          })
        }
      }
    })
  }
})